package com.dc.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.project.constant.ConstantCommon;
import com.dc.project.entity.bo.TaskDetailItemBo;
import com.dc.project.entity.map.TaskDetailItemMapVo;
import com.dc.project.entity.po.TaskDetailItem;
import com.dc.project.entity.query.TaskDetailItemQuery;
import com.dc.project.entity.vo.TaskDetailItemVo;
import com.dc.project.mapper.TaskDetailItemMapper;
import com.dc.project.service.TaskDetailItemService;
import com.dc.project.service.TemplateDetailService;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.util.list.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * TaskDetailItemServiceImpl
 *
 * @author chenxueli
 * @date 2025-03-10 10:55:12
 */
@Slf4j
@Service
public class TaskDetailItemServiceImpl extends AbstractBaseService<
        TaskDetailItem,
        TaskDetailItemBo,
        TaskDetailItemVo,
        TaskDetailItemMapVo,
        TaskDetailItemQuery,
        TaskDetailItemMapper
        > implements TaskDetailItemService {

    @Resource
    private TaskDetailItemMapper taskDetailItemMapper;

    @Resource
    private TemplateDetailService templateDetailService;

    /**
     * 根据任务id获取任务详情列表
     *
     * @param taskId 任务id
     * @return 任务详情列表
     */
    @Override
    public List<TaskDetailItem> listByTaskId(Long taskId) {
        var wrapper = lambdaQueryWrapper()
                .eq(TaskDetailItem::getFkTaskId, taskId)
                .orderByAsc(TaskDetailItem::getId);
        return listByWrapper(wrapper);
    }

    /**
     * 根据详情id获取任务详情列表
     *
     * @param detailId 详情id
     * @return 任务详情列表
     */
    @Override
    public List<TaskDetailItem> listByDetailId(Long detailId) {
        var wrapper = lambdaQueryWrapper()
                .eq(TaskDetailItem::getFkDetailId, detailId)
                .orderByAsc(TaskDetailItem::getId);
        return listByWrapper(wrapper);
    }

    /**
     * 保存分析内容
     *
     * @param taskId     任务id
     * @param detailId   任务详情id
     * @param contentMap 分析内容
     */
    @Override
    public void put(Long taskId, Long detailId, Map<String, String> contentMap) {
        var list = contentMap.entrySet().stream().map(item -> {
                    var po = new TaskDetailItem();
                    po.setFkTaskId(taskId);
                    po.setFkDetailId(detailId);
                    po.setTitle(item.getKey());
                    po.setType(ConstantCommon.Type.CREATE_BY_CUSTOM_FILE);
                    po.setDescription(item.getValue());
                    po.setGenerateStatus(TaskDetailItem.GenerateStatus.NORMAL);
                    return po;
                })
                .toList();
        // 新增信息
        ListUtil.groupList(list, 1000).forEach(taskDetailItemMapper::insertBatch);
    }

    /**
     * 根据模板key创建任务详情
     *
     * @param taskId      任务id
     * @param detailId    任务详情id
     * @param templateKey 模板key
     */
    @Override
    public void createByTemplateKey(Long taskId, Long detailId, String templateKey) {
        var detailList = templateDetailService.listByTemplateKey(templateKey);
        var list = detailList.stream().map(detail -> {
                    var item = new TaskDetailItem();
                    item.setFkTaskId(taskId);
                    item.setFkDetailId(detailId);
                    item.setType(ConstantCommon.Type.CREATE_BY_TEMPLATE);
                    item.setTemplateKey(templateKey);
                    item.setFkSourceId(detail.getId());
                    item.setTitle(detail.getTitle());
                    item.setDescription(detail.getDescription());
                    item.setGenerateStatus(TaskDetailItem.GenerateStatus.NORMAL);
                    return item;
                })
                .toList();
        // 新增信息
        ListUtil.groupList(list, 1000).forEach(taskDetailItemMapper::insertBatch);
    }

    /**
     * 获取最近的一条没有生成的数据
     *
     * @return 任务详情
     */
    @Override
    public TaskDetailItem getFirstNotHandleItem() {
        var wrapper = lambdaQueryWrapper()
                .eq(TaskDetailItem::getGenerateStatus, TaskDetailItem.GenerateStatus.WAITING)
                .orderByAsc(TaskDetailItem::getGmtModified)
                .orderByAsc(TaskDetailItem::getId);
        return getOneByWrapper(wrapper);
    }

    /**
     * 根据项目id更新状态
     *
     * @param itemId 项目id
     * @param status 状态
     */
    @Override
    public void updateStatusByItemId(Long itemId, int status) {
        var wrapper = new LambdaUpdateWrapper<TaskDetailItem>()
                .set(TaskDetailItem::getGenerateStatus, status)
                .set(TaskDetailItem::getGmtModified, LocalDateTime.now())
                .eq(TaskDetailItem::getId, itemId);
        taskDetailItemMapper.update(wrapper);
    }

    /**
     * 根据详情id更新状态
     *
     * @param detailId 详情id
     * @param status   状态
     */
    @Override
    public void updateStatusByDetailId(Long detailId, int status) {
        var wrapper = new LambdaUpdateWrapper<TaskDetailItem>()
                .set(TaskDetailItem::getGenerateStatus, status)
                .set(TaskDetailItem::getGmtModified, LocalDateTime.now())
                .eq(TaskDetailItem::getFkDetailId, detailId);
        taskDetailItemMapper.update(wrapper);
    }

    /**
     * 根据任务id更新状态
     *
     * @param taskId 任务id
     * @param status 状态
     */
    @Override
    public void updateStatusByTaskId(Long taskId, int status) {
        var wrapper = new LambdaUpdateWrapper<TaskDetailItem>()
                .set(TaskDetailItem::getGenerateStatus, status)
                .set(TaskDetailItem::getGmtModified, LocalDateTime.now())
                .eq(TaskDetailItem::getFkTaskId, taskId);
        taskDetailItemMapper.update(wrapper);
    }

}
